package net.openvision.music.model;

public class Length {
	
	public static Length NULL = new Length(0, 1);
	
	private int numerator;
	private int denominator;

	private int gcd(int a, int b) {
		int t = 0;
		while (b != 0) {
			t = b;
			b = a % b;
			a = t;
		}
		return a;
	}

	public static Length QUARTER = new Length(1, 4);

	public Length(int numerator, int denominator) {
		int gdc = gcd(numerator, denominator);

		this.numerator = numerator / gdc;
		this.denominator = denominator / gdc;
	}

	public int getNumerator() {
		return numerator;
	}

	public int getDenominator() {
		return denominator;
	}
	
	public boolean isNull() {
		return numerator == 0;
	}

	public double getValue() {
		return (double) numerator / (double) denominator;
	}
	
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Length))
			return false;
		Length l = (Length)o;
		return getValue() == l.getValue();
	}
	
	@Override
	public int hashCode() {
		return ((Double)getValue()).hashCode();
	}
}
